#ifndef __TINYSRV_PROCESSING_QUEUE_H__
#define __TINYSRV_PROCESSING_QUEUE_H__

#include "non_copyable.h"
#include "iprocessing_queue_callback.h"
#include "thread.h"
#include "cond_var.h"
#include "spinlock.h"

#include <vector>
#include <queue>

namespace tinySrv
{
  
  class ProcessingQueue
    : private NonCopyable
  {
  public:
    typedef IProcessingQueueCallback::IRefObjectPtr IRefObjectPtr;
    
    ProcessingQueue(unsigned threadCount, IProcessingQueueCallback *callback);
    ~ProcessingQueue();    
    void Push(IRefObjectPtr item);
    
  private:
    bool volatile IsRun;
    IProcessingQueueCallback *Callback;
    Spinlock ItemsGuard;
    CondVar ProcessingConditional;
    typedef std::queue<IRefObjectPtr> ItemPool;
    ItemPool Items;
    typedef std::vector<Thread *> ThreadPool;
    ThreadPool Threads;
    
    void DestroyThreads(ThreadPool *threads) const;
    void ThreadFunc();
  };

}

#endif  // !__TINYSRV_PROCESSING_QUEUE_H__
